import os
from urllib.request import urlretrieve
import requests
from bs4 import BeautifulSoup

downloadDirectory='6.1.2.downloaded'
#http://pythonscraping.com
baseurl='http://pythonscraping.com'

def getabsoluteurl(baseurl,source):
    if source.startswith('http://www.'):
        url='http://{}'.format(source[11:])
    elif source.startswith('http://'):
        url=source
    elif source.startswith('www.'):
        url=source[4:]
        url='http://{}'.format(source)
    else:
        url='http://{}'.format(source[8:])
    if baseurl not in url:
        return None
    return url

def getdownloadPath(baseurl,absoluteurl,downloadDirectory):
    path=absoluteurl.replace('www.','')
    path=path.replace(baseurl,'')
    path=downloadDirectory+path
    directory=os.path.dirname(path)
    if not os.path.exists(directory):
        os.makedirs(directory)
    return path

html=requests.get('http://www.pythonscraping.com')
bs=BeautifulSoup(html.text,'html.parser')
downloadlist=bs.find_all(src=True)
for download in downloadlist:
    fileurl=getabsoluteurl(baseurl,download['src'])
    if fileurl is not None:
        urlretrieve(fileurl,getdownloadPath(baseurl,fileurl,downloadDirectory))
